
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jy
 */
public class ExcuteInSwingWorker extends SwingWorker<String, Object> {
    private String m_str =null;
    private JTextArea m_jtx = null;
    private int meathod = -1; //选择用哪个算法进行分词
    private String m_result = " ";
    private long usedTime = 0;
    public ExcuteInSwingWorker(){}
    
    public ExcuteInSwingWorker(String str, JTextArea jtx, int meathod)
    {
        m_str = str;
        m_jtx = jtx;
        this.meathod = meathod;
    }
    
    @Override
    protected String doInBackground()
    {
        long startTime = System.currentTimeMillis();
        Segment cut = null;
        switch(meathod) {
	        case 0:  cut = new MaxMatching(3);
	        	break;
	        case 1:	 cut = new ReverseMaxMatching(3);
	        	break;
	        case 2:  cut = new MaxProbability();
	        	break;
        }
        cut.segment(m_str);
        
        long endTime = System.currentTimeMillis();
        setUsedTime(endTime - startTime);
        
        return cut.getResult();
    }
    
    protected void done()
    {
        try{
            m_jtx.setText(get());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    private void setUsedTime(long used)
    {
        usedTime = used;
    }
    
    public long getUsedTime()
    {
        return usedTime;
    }
}
